/*
 * Copyright 2008 Federal Emergency Management Agency
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */
package gov.fema.hazus.bdc.client.hazards.earthquake.buildingdesign;

import gov.fema.hazus.bdc.client.common.ui.AbstractForm;
import gov.fema.hazus.bdc.client.common.ui.BuildingUpdate;
import gov.fema.hazus.bdc.client.common.ui.TextBoxDoubleValueBlurHandler;
import gov.fema.hazus.bdc.client.common.ui.TextBoxIntegerValueBlurHandler;
import gov.fema.hazus.bdc.client.hazards.earthquake.EarthquakeConstants;
import gov.fema.hazus.bdc.shared.entity.Building;
import gov.fema.hazus.bdc.shared.entity.EarthquakeBuildingDesign;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class EarthquakeBuildingDesignForm extends AbstractForm implements BuildingUpdate {

	private Label designCode;
	private Label designLevel;
	private Label designYear;
	private Label designBaseShear;
	private Label foundationType;
	private Label constructionQuality;
	private Label percentageContentsOnGround;
	
	// form fields
	private EarthquakeDesignCodeListBox formDesignCode = new EarthquakeDesignCodeListBox();
	private EarthquakeDesignLevelListBox formDesignLevel = new EarthquakeDesignLevelListBox();
	private TextBox formDesignYear = new TextBox();
	private TextBox formDesignBaseShear= new TextBox();
	private EarthquakeFoundationTypeListBox formFoundationType= new EarthquakeFoundationTypeListBox();
	private EarthquakeConstructionQualityListBox formConstructionQuality= new EarthquakeConstructionQualityListBox();
	private TextBox formPercentageContentsOnGround= new TextBox();

	// ui components
	private DockPanel docker;
	private Grid table = new Grid(7,2);		
	
	// entities
	private EarthquakeBuildingDesign earthquakeBuildingDesign;
	
	// listeners
	private EarthquakeBuildingDesignListBoxChangeListener earthquakeBuildingDesignListBoxChangeListener;
	private EarthquakeBuildingDesignTextBoxKeyListener earthquakeBuildingDesignTextBoxKeyListener;
	private TextBoxDoubleValueBlurHandler textBoxDoubleValueBlurHandler;
	private TextBoxIntegerValueBlurHandler textBoxIntegerValueBlurHandler;

	private EarthquakeConstants constants;
	
	public EarthquakeBuildingDesignForm(Building building)
	{
		// do parent
		super(building);
		
		// set up widgets
		docker = new DockPanel();
		
		// set up tab title
		constants = (EarthquakeConstants)GWT.create(EarthquakeConstants.class);
		tabTitle = constants.earthquakeBuildingDesignTabTitle();
		
		designCode = new Label(constants.earthquakeBuildingDesignDesignCode());		
		designLevel = new Label(constants.earthquakeBuildingDesignDesignLevel());
		designYear = new Label(constants.earthquakeBuildingDesignDesignYear());
		designBaseShear = new Label(constants.earthquakeBuildingDesignDesignBaseShear());
		foundationType = new Label(constants.earthquakeBuildingDesignFoundationType());
		constructionQuality = new Label(constants.earthquakeBuildingDesignConstructionQuality());
		percentageContentsOnGround = new Label(constants.earthquakeBuildingDesignPercentageContentsOnGround());

		// add labels to form
		table.setWidget(0, 0, designCode);
		table.setWidget(1, 0, designLevel);
		table.setWidget(2, 0, designYear);
		table.setWidget(3, 0, designBaseShear);
		table.setWidget(4, 0, foundationType);
		table.setWidget(5, 0, constructionQuality);
		table.setWidget(6, 0, percentageContentsOnGround);

		// set up BuildingDesign
		earthquakeBuildingDesign = building.getEarthquakeBuildingDesign();
		earthquakeBuildingDesignListBoxChangeListener = new EarthquakeBuildingDesignListBoxChangeListener(earthquakeBuildingDesign);
		earthquakeBuildingDesignTextBoxKeyListener = new EarthquakeBuildingDesignTextBoxKeyListener(earthquakeBuildingDesign);
		textBoxDoubleValueBlurHandler = new TextBoxDoubleValueBlurHandler();
		textBoxIntegerValueBlurHandler = new TextBoxIntegerValueBlurHandler();
		
		// listeners
		loadListeners();
		
		// forms
		loadForms();
		
		
		// add fields to table
		table.setWidget(0, 1, formDesignCode);
		table.setWidget(1, 1, formDesignLevel);
		table.setWidget(2, 1, formDesignYear);
		table.setWidget(3, 1, formDesignBaseShear);
		table.setWidget(4, 1, formFoundationType);
		table.setWidget(5, 1, formConstructionQuality);
		table.setWidget(6, 1, formPercentageContentsOnGround);
		
		
		// add table to docker
		docker.add(table, DockPanel.CENTER);
		
		initWidget(docker);	
	}

	public void updateBuilding(Building building) {

		// entities
		setBuilding(building);
		this.earthquakeBuildingDesign = building.getEarthquakeBuildingDesign();
		
		// listeners
		earthquakeBuildingDesignListBoxChangeListener.updateEarthquakeBuildingDesign(earthquakeBuildingDesign);
		earthquakeBuildingDesignTextBoxKeyListener.updateEarthquakeBuildingDesign(earthquakeBuildingDesign);
		
		// reload forms
		loadForms();
		
	}
	
	private void loadForms() {
		
		if( earthquakeBuildingDesign != null )
		{
			formDesignCode.setListItem( earthquakeBuildingDesign.getDesignCode() );
			formDesignCode.setName(EarthquakeBuildingDesign.DESIGNCODE);
			
			formDesignLevel.setListItem( earthquakeBuildingDesign.getDesignLevel() );
			formDesignLevel.setName(EarthquakeBuildingDesign.DESIGNLEVEL);
			
			String designYear = null;
			if (earthquakeBuildingDesign.getDesignYear() != null) {
				designYear = earthquakeBuildingDesign.getDesignYear().toString();
			}
			formDesignYear.setText( designYear );
			formDesignYear.setName(EarthquakeBuildingDesign.DESIGNYEAR);
			
			String designBaseShear = null;
			if (earthquakeBuildingDesign.getDesignBaseShear() != null) {
				designBaseShear = earthquakeBuildingDesign.getDesignBaseShear().toString();
			}
			formDesignBaseShear.setText( designBaseShear );
			formDesignBaseShear.setName(EarthquakeBuildingDesign.DESIGNBASESHEAR);
			
			formFoundationType.setListItem( earthquakeBuildingDesign.getFoundationType() );
			formFoundationType.setName(EarthquakeBuildingDesign.FOUNDATIONTYPE);
			
			formConstructionQuality.setListItem( earthquakeBuildingDesign.getConstructionQuality() );
			formConstructionQuality.setName(EarthquakeBuildingDesign.CONSTRUCTIONQUALITY);
			
			String percentageContentsOnGround = null;
			if (earthquakeBuildingDesign.getPercentageContentsOnGround() != null) {
				percentageContentsOnGround = earthquakeBuildingDesign.getPercentageContentsOnGround().toString();
			} 
			formPercentageContentsOnGround.setText( percentageContentsOnGround );
			formPercentageContentsOnGround.setName(EarthquakeBuildingDesign.PERCENTAGECONTENTSONGROUND);
		}
		
		if( building != null)
		{
	        if( building.getMode().equalsIgnoreCase(Building.READ_ONLY))
	        {
	            formDesignCode.setEnabled(false);
	            formDesignLevel.setEnabled(false);
	            formDesignYear.setEnabled(false);
	            formDesignBaseShear.setEnabled(false);
	            formFoundationType.setEnabled(false);
	            formConstructionQuality.setEnabled(false);   
	            formPercentageContentsOnGround.setEnabled(false);            
	        }
	        else
	        {
	            formDesignCode.setEnabled(true);
	            formDesignLevel.setEnabled(true);
	            formDesignYear.setEnabled(true);
	            formDesignBaseShear.setEnabled(true);
	            formFoundationType.setEnabled(true);
	            formConstructionQuality.setEnabled(true);   
	            formPercentageContentsOnGround.setEnabled(true);   
	        }
		}
	}

	private void loadListeners() {
		formDesignCode.addChangeListener(earthquakeBuildingDesignListBoxChangeListener);
		formDesignLevel.addChangeListener(earthquakeBuildingDesignListBoxChangeListener);
		
		formDesignYear.addFocusListener(earthquakeBuildingDesignTextBoxKeyListener);
		formDesignYear.addBlurHandler(textBoxIntegerValueBlurHandler);
		
		formDesignBaseShear.addFocusListener(earthquakeBuildingDesignTextBoxKeyListener);
		formDesignBaseShear.addBlurHandler(textBoxDoubleValueBlurHandler);
		
		formFoundationType.addChangeListener(earthquakeBuildingDesignListBoxChangeListener);
		formConstructionQuality.addChangeListener(earthquakeBuildingDesignListBoxChangeListener);
		
		formPercentageContentsOnGround.addFocusListener(earthquakeBuildingDesignTextBoxKeyListener);
		formPercentageContentsOnGround.addBlurHandler(textBoxDoubleValueBlurHandler);
		
	}	
}
